package orthoplanner.com.google.code.eventhandlers;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import orthoplanner.com.google.code.ui.ImageViewer;


public class SaveAsImageHandler implements MouseListener {
	private ImageViewer imgViewer;
	private JFileChooser jfc;

	public SaveAsImageHandler(ImageViewer imgViewer) {
		this.imgViewer = imgViewer;
		jfc = new JFileChooser();
	}

	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void mousePressed(MouseEvent arg0) {
		try {
			jfc.setSelectedFile(new File(new File("Image.jpeg")
					.getCanonicalPath()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		int retValue = jfc.showSaveDialog(null);

		if (retValue == JFileChooser.APPROVE_OPTION) {

			String fName = jfc.getSelectedFile().getAbsolutePath();
			if (jfc.getSelectedFile().exists()) {
				if (jfc.getSelectedFile().delete()) {
					JOptionPane.showConfirmDialog(null,
							"An existing image overwritten");
					imgViewer.save(fName);
				} else {
					JOptionPane
							.showMessageDialog(
									null,
									"Unable to re-write the existing image, delet it first",
									"Error ", JOptionPane.ERROR_MESSAGE);
				}
			} else {
				imgViewer.save(fName);
			}
		}
	}

	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

}
